package com.qymerge.manage.system.controller;

import com.qymerge.manage.core.bean.*;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.controller.BaseController;
import com.qymerge.manage.core.utils.GsonUtil;
import com.qymerge.manage.core.utils.PrintCodeUtils;
import com.qymerge.manage.core.utils.RequestCheckUtils;
import com.qymerge.manage.system.dto.DatabackDto;
import com.qymerge.manage.system.ro.DatabackRO;
import com.qymerge.manage.system.service.IDatabackService;
import com.google.common.reflect.TypeToken;
import com.qymerge.manage.core.bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>Title: DBController</p>
 * <p>Description: </p>
 *
 * @Author cong.zhang
 * @Version 1.0.0
 * @Date 2020/12/23/0023
 * @Time 1:07
 */
@Controller
@RequestMapping(value = "/sys/databack")
public class DatabackController extends BaseController {

    @Autowired
    private IDatabackService databackService;

    /**
     * 列表
     *
     * @param parameters
     * @param callback
     */
    @RequestMapping(value = "/list")
    public void list(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<DatabackDto> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<DatabackDto>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        DatabackDto ro = paramValue.getData();

        DataTableRO data = databackService.list(ro, new PageRequest(common.getPage(), common.getSize()));
        PrintCodeUtils.printSuccess(response, data.build(), callback);
    }

    /**
     * 详情
     *
     * @param request
     * @param response
     * @param callback
     * @throws Exception
     */
    @RequestMapping("/detail")
    public void detail(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {

        ParamValue<DatabackRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<DatabackRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        DatabackRO ro = paramValue.getData();

        RequestCheckUtils.checkNotEmpty(ro.getId(),"ID必须");
        DatabackRO resultRO = databackService.detail(ro.getId());
        PrintCodeUtils.printSuccess(response, resultRO, callback);
    }

    /**
     * 增加
     *
     * @param request
     * @param response
     * @param callback
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {
        ParamValue<DatabackRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<DatabackRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        DatabackRO ro = paramValue.getData();

        ActionResultRO resultRO = databackService.save(ro);
        PrintCodeUtils.print(response, resultRO, callback);
    }

    /**
     * 删除
     *
     * @param parameters
     * @return
     */
    @RequestMapping(value = "/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<CommonIdsDTO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<CommonIdsDTO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        CommonIdsDTO commonIdsDTO = paramValue.getData();

        ActionResultRO obj = databackService.delete(commonIdsDTO.getIds());
        PrintCodeUtils.print(response, obj, callback);
    }


}
